{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "lesbian-springer",
   "metadata": {},
   "source": [
    "# Seldon V2 Pipeline DataFlow Batching Example\n",
    "\n",
    "\n",
    " * Build if needed and place `seldon` binary in your path\n",
    "    * run `make build-seldon` from operator folder and add bin folder to PATH\n",
    " * Run Seldon V2 `make deploy-local` from top level folder\n",
    " * Install `kafkacat` for debugging kafka topics & messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "modular-digest",
   "metadata": {},
   "outputs": [],
   "source": [
    "!which seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b78ea55e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -L | grep -i seldon -A1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "running-antarctica",
   "metadata": {},
   "source": [
    "## Model Batched Chaining based on batch size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f2ab2a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../samples/models/sklearn-iris-gs.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9e073d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon model load -f ../../samples/models/sklearn-iris-gs.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "997b4028",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon model status --model-name iris -w ModelAvailable | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d017ede",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../samples/pipelines/iris-batch.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "following-winning",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline load -f ../../samples/pipelines/iris-batch.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artistic-kentucky",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline status -p iris-batch -w PipelineReady | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c33133e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline infer -p iris-batch --inference-mode grpc \\\n",
    "    '{\"model_name\":\"iris\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4]},\"datatype\":\"INT32\",\"shape\":[1,4]}]}' | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c2f6d4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -t seldon.default.model.iris.outputs -C -o beginning -e -f 'Offset %o\\tlength %S\\tcontents %s\\n' -s value=i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93f472fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline infer -p iris-batch --inference-mode grpc \\\n",
    "    '{\"model_name\":\"iris\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4]},\"datatype\":\"INT32\",\"shape\":[1,4]}]}' | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd635024",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -L | grep -i seldon -A1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95f63878",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -t seldon.default.pipeline.iris-batch.inputs -C -o beginning -e -f 'Offset %o\\tlength %S\\tcontents %s\\n' -s value=i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c37ed57e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -t seldon.default.model.iris.inputs -C -o beginning -e -f 'Offset %o\\tlength %S\\tcontents %s\\n' -s value=i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d9dbbfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kafkacat -b localhost:9092 -t seldon.default.model.iris.outputs -C -o beginning -e -f 'Offset %o\\tlength %S\\tcontents %s\\n' -s value=i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e51e17f",
   "metadata": {},
   "source": [
    "### Cleanup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dimensional-hours",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon pipeline unload -p iris-batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "outside-inspiration",
   "metadata": {},
   "outputs": [],
   "source": [
    "!seldon model unload --model-name iris"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
